<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: actions.php 11 2013-09-21 11:43:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 11 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:43:11 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

switch($_GET['action']) {
	case 'add_product_options':
		for ($i = 0, $n = sizeof($languages); $i < $n; $i ++) {
			$option_name = $_POST['option_name'][$languages[$i]['id']];
			$option_desc = $_POST['option_desc'][$languages[$i]['id']];

			if($image = &try_upload('option_image_'.$languages[$i]['id'], DIR_FS_CATALOG_IMAGES.'product_options/')) {
				$paname_arr = explode('.',$image -> filename);
				$pnsuffix = array_pop($paname_arr);
				$option_image_name = 'opt_'.$_POST['products_options_id'].'_'. $languages[$i]['id'] .'.'.$pnsuffix;

				@unlink(DIR_FS_CATALOG_IMAGES.'product_options/'.$option_image_name);
				rename(DIR_FS_CATALOG_IMAGES.'product_options/'.$image->filename, DIR_FS_CATALOG_IMAGES.'product_options/'.$option_image_name);
			}

			$new_option_to_sql = array('products_options_id' => $_POST['products_options_id'],
										'products_options_name' => $option_name,
										'products_options_desc' => $option_desc,
										'products_options_image' => $option_image_name,
										'language_id' => $languages[$i]['id']);

			$db->db_perform(TABLE_PRODUCTS_OPTIONS, $new_option_to_sql);
		}
		$message_stack->add_session('Das neue Attribut wurde gespeichert', 'success');
		redirect(href_link(FILENAME_PRODUCTS_ATTRIBUTES, $opt_add.'#option'));
		break;

	case 'add_product_option_values':
		for ($i = 0, $n = sizeof($languages); $i <= $n; $i ++) {
			$value_name = $_POST['value_name'];
			$value_desc = $_POST['value_desc'];
			$value_hex = $_POST['hex_image'];
			$new_attribut_sql = '';

			if($image = &try_upload('value_image_'.$languages[$i]['id'], DIR_FS_CATALOG_IMAGES.'product_options/')) {
				$paname_arr = explode('.',$image -> filename);
				$pnsuffix = array_pop($paname_arr);
				$value_image_name = 'val_'.$_POST['value_id'].'_'. $languages[$i]['id'] .'.'.$pnsuffix;

				@unlink(DIR_FS_CATALOG_IMAGES.'product_options/'.$value_image_name);
				rename(DIR_FS_CATALOG_IMAGES.'product_options/'.$image->filename, DIR_FS_CATALOG_IMAGES.'product_options/'.$value_image_name);
			}

			$new_attribut_sql = array(	'products_options_values_id' => $_POST['value_id'],
										'language_id' => $languages[$i]['id'],
										'products_options_values_name' => $value_name[$languages[$i]['id']],
										'products_options_values_desc' => $value_desc[$languages[$i]['id']],
										'products_options_hex_image' => $value_hex[$languages[$i]['id']],
										'products_options_values_image' => $value_image_name);
			
			$db->db_perform(TABLE_PRODUCTS_OPTIONS_VALUES, $new_attribut_sql);
		}

		$new_value_to_option_sql = array('products_options_id' => $_POST['option_id'], 'products_options_values_id' => $_POST['value_id']);
		$db->db_perform(TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS, $new_value_to_option_sql);
		
		$message_stack->add_session('Der neue Attributwert wurde gespeichert', 'success');
		redirect(href_link(FILENAME_PRODUCTS_ATTRIBUTES, $val_add.'&value_tab=1'));
		break;

	case 'update_option_name':
		$languages = get_languages();
		for ($i = 0, $n = sizeof($languages); $i < $n; $i ++) {
			$option_name = $_POST['option_name'][$languages[$i]['id']];
			$option_desc = $_POST['option_desc'][$languages[$i]['id']];

			if($_FILES['option_image_'.$languages[$i]['id']]['name'] != '') {
				if ($image = &try_upload('option_image_'.$languages[$i]['id'], DIR_FS_CATALOG_IMAGES.'product_options/')) {
					$paname_arr = explode('.', $image->filename);
					$pnsuffix = array_pop($paname_arr);
					$option_image_name = 'opt_'.$_POST['option_id'].'_'. $languages[$i]['id'] .'.'.$pnsuffix;

					@unlink(DIR_FS_CATALOG_IMAGES.'product_options/'.$_POST['previous_opt_image'][$languages[$i]['id']]);
					rename(DIR_FS_CATALOG_IMAGES.'product_options/'.$image->filename, DIR_FS_CATALOG_IMAGES.'product_options/'.$option_image_name);

					$image_sql = array('products_options_image' => $option_image_name);
				}
			} elseif($_POST['del_option_image_'. $languages[$i]['id']] == 'yes') { // Optionsbild löschen
				$image_sql = array('products_options_image' => '');
				@unlink(DIR_FS_CATALOG_IMAGES.'product_options/'.$_POST['previous_opt_image'][$languages[$i]['id']]);
			} else
				$image_sql = '';

			$update_option_sql = array('products_options_name' => $option_name,
										'products_options_desc' => $option_desc);

			if(!empty($image_sql))
				$update_option_sql = array_merge($update_option_sql, $image_sql);

			$db->db_perform(TABLE_PRODUCTS_OPTIONS, $update_option_sql, 'update', "products_options_id = '".$_POST['option_id']."' AND language_id = '".$languages[$i]['id']."' ");
		}
		redirect(href_link(FILENAME_PRODUCTS_ATTRIBUTES, $opt_add.'#option'));
		break;

	case 'update_value':
		$languages = get_languages();

		for($i = 0, $n = sizeof($languages); $i < $n; $i ++) {
			$value_name = $_POST['value_name'][$languages[$i]['id']];
			$value_desc = $_POST['value_desc'][$languages[$i]['id']];
			$value_hex = $_POST['hex_image'][$languages[$i]['id']];

			if($_FILES['value_image_'.$languages[$i]['id']]['name'] != '') {
				if ($image = &try_upload('value_image_'.$languages[$i]['id'], DIR_FS_CATALOG_IMAGES.'product_options/')) {
					$paname_arr = explode('.',$image -> filename);
					$pnsuffix = array_pop($paname_arr);
					$value_image_name = $_POST['value_id'].'_'.$languages[$i]['id'].'.'.$pnsuffix;

					@unlink(DIR_FS_CATALOG_IMAGES.'product_options/'.$_POST['previous_val_image'][$languages[$i]['id']]);
					rename(DIR_FS_CATALOG_IMAGES.'product_options/'.$image -> filename, DIR_FS_CATALOG_IMAGES.'product_options/'.$value_image_name);

					$image_sql = array('products_options_values_image' => $value_image_name);
				}

			} elseif($_POST['del_value_image_'. $languages[$i]['id']] == 'yes') { // bild loeschen
				$image_sql = array('products_options_values_image' => '');
				@unlink(DIR_FS_CATALOG_IMAGES.'product_options/'.$_POST['previous_val_image'][$languages[$i]['id']]);

			} else
				$image_sql = '';

			$update_value_sql = array(	'products_options_values_name' => $value_name,
										'products_options_hex_image' => $value_hex,
										'products_options_values_desc' => $value_desc);

			if(!empty($image_sql))
				$update_value_sql = array_merge($update_value_sql, $image_sql);

			$db->db_perform(TABLE_PRODUCTS_OPTIONS_VALUES, $update_value_sql, 'update', "products_options_values_id = '".$_POST['value_id']."' AND language_id = '".$languages[$i]['id']."'");
		}

		$db->db_query("UPDATE ".TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS." SET products_options_id = '".$_POST['option_id']."' WHERE products_options_values_id = '".$_POST['value_id']."'");
		redirect(href_link(FILENAME_PRODUCTS_ATTRIBUTES, $val_add.'&value_tab=1'));
		break;

	case 'delete_option':
		$image = $db->db_query("SELECT
									products_options_image
								FROM
									".TABLE_PRODUCTS_OPTIONS."
								WHERE
									products_options_id = '".$_GET['option_id']."'");
		
		if(!empty($image->fields['products_options_image']))
			@unlink(DIR_FS_CATALOG_IMAGES.'product_options/'.$image->fields['products_options_image']);
		
		$del_options = $db->db_query("SELECT
											products_options_values_id
										FROM
											".TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS."
										WHERE
											products_options_id = '".$_GET['option_id']."'");

		while(!$del_options->EOF) {
			$db->db_query("DELETE FROM
								".TABLE_PRODUCTS_OPTIONS_VALUES."
							WHERE
								products_options_values_id = '".$_GET['option_id']."'");
			$del_options->MoveNext();
		}
		$db->db_query("DELETE FROM
							".TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS."
						WHERE
							products_options_id = '".$_GET['option_id']."'");
		$db->db_query("DELETE FROM
							".TABLE_PRODUCTS_OPTIONS."
						WHERE
							products_options_id = '".$_GET['option_id']."'");

		redirect(href_link(FILENAME_PRODUCTS_ATTRIBUTES, $opt_add.'#option'));
		break;

	case 'delete_value':
		$image = $db->db_query("SELECT
									products_options_values_image
								FROM
									".TABLE_PRODUCTS_OPTIONS_VALUES."
								WHERE
									products_options_values_id = '".$_GET['value_id']."'");
		
		if(!empty($image->fields['products_options_values_image']))
			@unlink(DIR_FS_CATALOG_IMAGES.'product_options/'.$image->fields['products_options_values_image']);

		$db->db_query("DELETE FROM ".TABLE_PRODUCTS_OPTIONS_VALUES." WHERE products_options_values_id = '".$_GET['value_id']."'");
		$db->db_query("DELETE FROM ".TABLE_PRODUCTS_OPTIONS_VALUES." WHERE products_options_values_id = '".$_GET['value_id']."'");
		$db->db_query("DELETE FROM
							".TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS."
						WHERE
							products_options_values_id = '".$_GET['value_id']."'");
		redirect(href_link(FILENAME_PRODUCTS_ATTRIBUTES, $val_add.'&value_tab=1'));
		break;
}